//
//  WCNFirstBodyPartVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/25.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNFirstBodyPartVC.h"
#import "WCNSecondBodyPartVC.h"
//
#import "MJRefresh.h"
//
#import "Patient.h"
#import "WCNetworkAccessManager.h"

@interface WCNFirstBodyPartVC ()<UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) Patient *patient;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *sexSwitchButton;
@property (weak, nonatomic) IBOutlet UILabel *sexStringLabel;


@property (strong, nonatomic) NSArray *bodyParts;

@end

@implementation WCNFirstBodyPartVC

- (instancetype)initWithPatient:(Patient *)patient
{
    self = [super init];
    if (self) {
        self.title = @"伤口部位";
        
        _patient = patient;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSString *sexStr = self.patient.sex;
    if ([@"MALE" isEqualToString:sexStr]) {
        self.sexStringLabel.text = @"男";
        self.sexSwitchButton.selected = NO;
    } else {
        self.sexStringLabel.text = @"女";
        self.sexSwitchButton.selected = YES;
    }
    self.sexSwitchButton.enabled = NO;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
    
    __weak typeof(self) weakSelf = self;
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [weakSelf refresh];
    }];
    [self.tableView.header beginRefreshing];
}

- (void)refresh
{
    NSString *url;
    if (self.sexSwitchButton.selected) {
        url = @"wound/bodypart/first/WOMAN";
    } else {
        url = @"wound/bodypart/first/MAN";
    }
    
    [[WCNetworkAccessManager sharedMg] GET:url parameters:nil success:^(NSDictionary *result){
        self.bodyParts = result[@"data"];
        [self.tableView reloadData];
    } failure:nil netError:nil always:^{
        [self.tableView.header endRefreshing];
    }];
}

- (IBAction)sexSwitch:(UIButton *)sender
{
    BOOL selected = sender.isSelected;
    if (selected) {
        self.sexStringLabel.text = @"男";
        sender.selected = NO;
    } else {
        self.sexStringLabel.text = @"女";
        sender.selected = YES;
    }
    
    [self.tableView.header beginRefreshing];
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    
    NSDictionary *bodyPart = self.bodyParts[indexPath.row];
    cell.textLabel.text = bodyPart[@"partName"];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    [cell setSeparatorInset:UIEdgeInsetsZero];
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.bodyParts.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    WCNSecondBodyPartVC *vc = [[WCNSecondBodyPartVC alloc] initWithFirstPart:self.bodyParts[indexPath.row]];
    vc.wound = self.wound;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
